﻿using System;
using System.ServiceModel;

namespace ClusterTest
{
    [ServiceContract]
    public interface ITestClusterService
    {
        [OperationContract]
        void Replicate(string name);

        [OperationContract]
        void DiscardReplication(string name);
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    class TestClusterService : ITestClusterService
    {
        private readonly TestClusterManager cm;

        public TestClusterService(TestClusterManager cm)
        {
            this.cm = cm;
        }

        public void Replicate(string name)
        {
            cm.RegisterServerReplication(name);
        }

        public void DiscardReplication(string name)
        {
            if (name == cm.ServerName)
            {
                throw new FaultException("Can't discard replication for self");
            }

            cm.DiscardWasCalled = true;
            cm.UnregisterReplicatedServerBySelf(name);
        }
    }
}
